import AddressPicker from "@/components/lingdang-AddressPicker/AddressPicker.vue"
import {
	isPhone
} from '@/utils/verify';
export default {
	components: {
		AddressPicker
	},
	data() {
		return {
			nickname: '',
			phone: '',
			address: '',
			regionText: '请选择',
			province: '',
			city: '',
			region: '',
			isAdd: true,
			isDefault: false,
			addrId: '',
			isOrder: 0
		};
	},
	onLoad(options) {
		if (options.addr) {
			this.isAdd = false
			this.addrId = options.addr
			uni.setNavigationBarTitle({
				title: "修改地址"
			})
			this.getAddrDetail()
		} else {
			this.isAdd = true
			uni.setNavigationBarTitle({
				title: "新增地址"
			})
		}

		if (options.isOrder) {
			// 从提交订单进入
			this.isOrder = options.isOrder
		}
	},
	methods: {
		// 选择地区
		onCityChange: function(result) {
			console.log(JSON.stringify(result.data));
			this.regionText = '';
			result.data.forEach((item, index) => {
				if (index === 0) {
					this.regionText += item.name
				} else {
					this.regionText += '-' + item.name
				}
			});
			let arr = this.regionText.split('-');
			console.log(arr)
			this.province = arr[0];
			this.city = arr[1];
			this.region = arr[2];
		},
		// 设置默认
		onSwitch(value) {
			this.isDefault = value
		},
		// 获取地址详情
		async getAddrDetail() {
			var res = await this.$get('server/index/getAddress', {
				id: this.addrId
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.nickname = res.res.name
			this.phone = res.res.phone
			this.province = res.res.province
			this.city = res.res.city
			this.region = res.res.region
			this.address = res.res.address
			this.regionText = `${this.province}-${this.city}-${this.region}`
			this.isDefault = res.res.is_default == 1
		},
		onConfirm() {
			if (!this.nickname) {
				this.$toast('请输入收货人姓名')
				return
			}
			if (!this.phone) {
				this.$toast('请输入手机号码')
				return
			}
			if (!isPhone(this.phone)) {
				this.$toast('请输入正确的手机号');
				return;
			}
			if (this.regionText == '请选择') {
				this.$toast('请选择地区')
				return
			}
			if (!this.address) {
				this.$toast('请输入详细地址')
				return
			}
			var data = {
				name: this.nickname,
				phone: this.phone,
				province: this.province,
				city: this.city,
				region: this.region,
				address: this.address,
				is_default: this.isDefault ? 1 : 0
			}
			if (this.isAdd) {
				this.addAddr(data)
			} else {
				data.id = this.addrId
				this.editAddr(data)
			}

		},
		// 添加地址
		async addAddr(data) {
			var res = await this.$post('server/index/addAddress', data);
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('添加成功')
			setTimeout(() => {
				uni.navigateBack()
			}, 1000)

			if(this.isOrder == 1){
				uni.$emit('chooseAddr', {
					addr: res.data.address
				});
			}
		},
		// 编辑地址
		async editAddr(data) {
			var res = await this.$post('server/index/editAddress', data);
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('修改成功')
			setTimeout(() => {
				uni.navigateBack()
			}, 1000)
		},
		// 删除地址
		async onDelect() {
			var res = await this.$get('server/index/delAddress', {
				id: this.addrId
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('删除成功')
			setTimeout(() => {
				uni.navigateBack()
			}, 1000)
		}
	}
}
